package 单周赛.history;

import java.util.Arrays;

/**
 *
 */
public class 第285场单周赛 {

    public static void main(String[] args) {

        int[] nums = {6, 6, 5, 5, 4, 1};
        System.out.println(countHillValley(nums));
        //                                               1  345 67 89  11
        System.out.println(countCollisions("SSRSSRLLRSLLRSRSSRLRRRRLLRRLSSRR"));

        int[] aliceArrows = {1, 1, 0, 1, 0, 0, 2, 1, 0, 1, 2, 0};

        System.out.println(maximumBobPoints(3, aliceArrows));
    }

    /**
     * 6027. 统计数组中峰和谷的数量
     * 思路：
     * 模拟
     *
     * @param nums
     * @return
     */
    public static int countHillValley(int[] nums) {
        int ans = 0;
        int cur = 1;
        while (cur < nums.length - 1) {
            int curNum = nums[cur];
            int left = cur - 1;
            int right = cur + 1;
            while (left >= 0 && nums[left] == curNum) {
                left--;
            }
            while (right <= nums.length - 1 && nums[right] == curNum) {
                right++;
            }
            if (left < 0 || right > nums.length - 1) {  // 注意：如果当前元素左边或者右边都是相等元素，那么它一定不是峰或谷
                cur++;
                continue;
            }
            if (nums[right] > curNum && nums[left] > curNum ||
                    nums[right] < curNum && nums[left] < curNum) {
                ans++;  // 相邻的谷或峰只能算一个
                cur = right;
            } else {
                cur++;
            }
        }
        return ans;
    }

    /**
     * 6028. 统计道路上的碰撞次数
     * 分类讨论
     * RL -> +2 -> SS
     * SL -> +1 -> SS
     * RS -> +1 -> SS
     *
     * @param directions
     * @return
     */
    public static int countCollisions(String directions) {
        int ans = 0;
        char[] chars = directions.toCharArray();
        for (int i = 0; i < chars.length - 1; i++) {
            if (chars[i] == 'R' && chars[i + 1] == 'L') {
                ans += 2;
                chars[i] = 'S';
                chars[i + 1] = 'S';
            } else if (chars[i] == 'S' && chars[i + 1] == 'L') {
                ans++;
                chars[i + 1] = 'S';
            } else if (chars[i] == 'R' && chars[i + 1] == 'S') {
                ans++;
                chars[i] = 'S';
            }
        }
        // 注意：正向遍历碰到RRRS，由于速度一样，右边停车，也会影响到左边RR的车辆
        for (int i = chars.length - 2; i >= 0; i--) {
            if (chars[i] == 'R' && chars[i + 1] == 'S') {
                ans++;
                chars[i] = 'S';
            }
        }
        return ans;
    }

    /**
     * 6029. 射箭比赛中的最大得分
     * 贪心思想？？？
     * 先求出每个得分位置的每支箭的性价比
     *
     * @param numArrows
     * @param aliceArrows
     * @return
     */
    public static int[] maximumBobPoints(int numArrows, int[] aliceArrows) {
        int[] bobArrows = new int[aliceArrows.length];
        double[][] prices = new double[12][2];
        for (int score = 0; score < prices.length; score++) {
            double price = aliceArrows[score] == 0 ? score : (double) score / aliceArrows[score];
            prices[score] = new double[]{score, price};
        }
        Arrays.sort(prices, (o1, o2) -> compare(o2[1], o1[1]));
        int[] flags = new int[12];
        for (double[] price : prices) {
            int index = (int) price[0];
            if (flags[index] == 1) {
                continue;
            }
            if (numArrows >= aliceArrows[index]) {
                numArrows -= aliceArrows[index];
                bobArrows[(int) price[0]] = aliceArrows[index];
                flags[(int) price[0]] = 1;
            }
        }

        return bobArrows;
    }

    private static int compare(double o1, double o2) {
        if (o1 - o2 > 0) {
            return 1;
        } else if (o1 - o2 < 0) {
            return -1;
        } else {
            return 0;
        }
    }

}
